#include <QtGui>
#include "customsqlmodel.h"

CustomSqlModel::CustomSqlModel(QObject *parent)
    : QSqlQueryModel(parent)
{
}

QVariant CustomSqlModel::data(const QModelIndex& aIndex, int aRole) const
{
    QVariant data = QSqlQueryModel::data(aIndex, aRole);

    if(!aIndex.isValid())
        return data;

    int col = aIndex.column();

    if(aRole == Qt::DecorationRole && col==5){ // status column
        // get the cell value, from DisplayRole
        QString status = QSqlQueryModel::data(aIndex, Qt::DisplayRole).toString();
        if(status=="open")
            return QIcon(":/images/red.png");
        if(status=="working")
            return QIcon(":/images/green.png");
        if(status=="waiting")
            return QIcon(":/images/yellow.png");
        if(status=="closed")
            return QIcon(":/images/gray.png");
    }

    // checkbox column
    //if(aRole == Qt::CheckStateRole and col==7){ // active column
    //    int active = QSqlQueryModel::data(aIndex, Qt::DisplayRole).toInt();
    //    if(active)
    //        return Qt::Checked;
    //    else
    //       return Qt::Unchecked;
    //}


    // display data
    if(aRole == Qt::DisplayRole)
    {
        if(col==0) // number zero-padding
            return data.toString().prepend("000000").right(7);

        if(col==6) // active column, change displayed data
            return (data.toInt()>0)?"YES":"NO";

        // cut long lines in every cell with text longer than 15 characters
        if(data.toString().size()>15)
            return data.toString().mid(0,15).append("...");

    }

    return data;		// some other role we are ignoring
}

